varying vec4 mycolor;
varying vec3 mynormal;
varying vec4 myvertex;
varying vec3 mytangent;

uniform vec4 mylight_position;
uniform vec4 mylight_color;
uniform vec3 mylight_direction;
uniform int  mylight_type;
uniform int  render_type;

uniform sampler2D colorTexture;
uniform sampler2D normalTexture;
uniform samplerCube cubeTexture;

void main (void) 
{        
	int type = mylight_type;

	vec3 eyepos = vec3(0,0,0) ; 
	
	vec4 _mypos = gl_ModelViewMatrix * myvertex ; 
    vec3 mypos = _mypos.xyz / _mypos.w ;

	//vec4 _lightpos = gl_ModelViewMatrix * mylight_position;
	vec4 _lightpos = mylight_position;
   	vec3 lightpos = _lightpos.xyz / _lightpos.w;

	vec3 normal = normalize(gl_NormalMatrix*mynormal);
	



	vec4 diffuse;
	vec4 ambient;
	vec4 specular;
	int shininess;

	if ( render_type == 0 )
	{
		diffuse = gl_FrontMaterial.diffuse;
		ambient = gl_FrontMaterial.ambient;
		specular = gl_FrontMaterial.specular;
		shininess =  gl_FrontMaterial.shininess;
	}
	else if ( render_type == 1 )
	{
		diffuse = texture2D(colorTexture, gl_TexCoord[0].st);
		ambient = vec4(0,0,0,1) * diffuse;
		specular = gl_FrontMaterial.specular;//specular = vec4(0.3,0.3,0.3,1);
		shininess =  gl_FrontMaterial.shininess;//shininess = 100;
	}
	else if ( render_type == 2 )
	{
		diffuse = texture2D(colorTexture, gl_TexCoord[0].st);
		ambient = vec4(0,0,0,1) * diffuse;
		specular =  gl_FrontMaterial.specular;//specular = vec4(0.3,0.3,0.3,1);
		shininess = gl_FrontMaterial.shininess;//shininess = 100;

		vec3 tangent = normalize(gl_NormalMatrix*mytangent);
		vec3 bitangents = normalize(cross(normal,tangent));
		mat3 mat = mat3(tangent,bitangents,normal);
		mat3 tmat = transpose(mat);

		//lightpos = tmat * lightpos;
		//eyepos = tmat *eyepos;
		normal = normalize(2.0 * texture2D(normalTexture, gl_TexCoord[0].st).xyz-1);
	}
	else if ( render_type == 3 )
	{
		vec3 reflectdir = normalize( reflect( normalize(mypos-eyepos) , normal ) );
		reflectdir = inverse(gl_NormalMatrix)*reflectdir;

		diffuse = textureCube(cubeTexture, reflectdir);
		ambient = vec4(0,0,0,1) * diffuse;
		specular =  gl_FrontMaterial.specular;//specular = vec4(0.3,0.3,0.3,1);
		shininess = gl_FrontMaterial.shininess;//shininess = 100;
	}

	vec3 eyedir = normalize(eyepos - mypos) ;
		if ( dot(normal,eyedir) < 0 )
		normal = -normal;


    //point-lights
	if (type == 1) {
		vec3 lightdir = normalize (lightpos - mypos) ;
		vec3 reflectdir = normalize( reflect(-lightdir, normal) );

		gl_FragColor =  ambient + 
					    diffuse * mylight_color * max( dot(lightdir, normal), 0.0) +
					    specular * mylight_color * pow( max( dot(reflectdir, eyedir), 0.0 ), shininess );
    } 
	//directinal lights
	else if (type == 2)	{
		vec3 lightdir = normalize (-normalize(gl_NormalMatrix*mylight_direction)) ;
		vec3 reflectdir = normalize( reflect(-lightdir, normal) );

		gl_FragColor =  ambient + 
					    diffuse * mylight_color * max( dot(lightdir, normal), 0.0) +
					    specular * mylight_color * pow( max( dot(reflectdir, eyedir), 0.0 ), shininess );
	} 
	//spot-lights
	else if (type == 3)
	{
		vec3 spotDir = normalize (-normalize(gl_NormalMatrix*mylight_direction));
		vec3 lightDir = normalize (lightpos - mypos);
		vec3 reflectdir = normalize( reflect(-spotDir, normal) );

		gl_FragColor =  ambient + 
					    diffuse * mylight_color * max( dot(lightDir, normal), 0.0) +
					    specular * mylight_color * pow( max( dot(reflectdir, eyedir), 0.0 ), shininess );
		
		gl_FragColor =	gl_FragColor*pow(max(dot(-lightDir, normalize(spotDir)),0.0),2000);
	}
	else 	gl_FragColor=mycolor;

}